package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.LessonModel;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyli on 2017/10/4.
 */

public class LessonAdapter extends BaseAdapter {

    private Context context;
    private List<LessonModel.DataBean> dataList = new ArrayList<>();
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;


    public LessonAdapter(Context context, List<LessonModel.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }


    public void addRes(List<LessonModel.DataBean> data) {
        if (data != null) {
//            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void refresh(List<LessonModel.DataBean> data) {
        if (data != null) {
            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LessonModel.DataBean bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.fragment_lesson_listview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.lessonTitle.setText(bean.getTitle());
        viewHolder.lessonPrice.setText("¥" + bean.getPrice());
        viewHolder.lessonTime.setText(bean.getNew_startTime());
        viewHolder.lessonSale.setText("已购" + bean.getStudentNum());
        viewHolder.lessonXiangou.setText("限购" + bean.getMaxStudentNum());
        imageLoader.displayImage(bean.getLargePicture(), viewHolder.lessonImg, options);

        return view;
    }


    class ViewHolder {
        ImageView lessonImg;
        TextView lessonTitle;
        TextView lessonTime;
        TextView lessonSale;
        TextView lessonPrice;
        TextView lessonXiangou;

        public ViewHolder(View view) {
            lessonImg = view.findViewById(R.id.lesson_img);
            lessonTitle = view.findViewById(R.id.lesson_title);
            lessonPrice = view.findViewById(R.id.lesson_price);
            lessonTime = view.findViewById(R.id.lesson_time);
            lessonSale = view.findViewById(R.id.lesson_sale);
            lessonXiangou = view.findViewById(R.id.lesson_xiangou);
        }
    }

}
